/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BuscarCliente.java
 *
 * Created on 12-oct-2011, 19:16:41
 */
package interfaces;

import dominio.Cliente;
import javax.swing.JOptionPane;

/**
 *
 * @author a1
 */
public class BuscarCliente extends javax.swing.JDialog {

    Principal parent;
    Cliente cliente;
    /** Creates new form BuscarCliente */
    public BuscarCliente(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.parent=(Principal) parent;
        cliente=null;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jtfNumero = new javax.swing.JTextField();
        jbBuscar = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jtfSaldo = new javax.swing.JTextField();
        jbGuardarCambios = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jLabel1.setText("Ingresar Numero de Cliente: ");

        jbBuscar.setText("Buscar");
        jbBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbBuscarActionPerformed(evt);
            }
        });

        jLabel4.setText("Modificar Saldo :");

        jtfSaldo.setEnabled(false);

        jbGuardarCambios.setText("Guardar Cambios");
        jbGuardarCambios.setEnabled(false);
        jbGuardarCambios.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbGuardarCambiosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(149, 149, 149)
                        .addComponent(jbBuscar))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jtfNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(18, 18, 18)
                                .addComponent(jtfSaldo, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(136, 136, 136)
                        .addComponent(jbGuardarCambios)))
                .addContainerGap(71, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addComponent(jbBuscar)
                .addGap(60, 60, 60)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jtfSaldo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37)
                .addComponent(jbGuardarCambios)
                .addContainerGap(62, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jbGuardarCambiosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbGuardarCambiosActionPerformed
    this.parent.eliminarCliente(cliente);
    this.parent.agregarCliente(new Cliente(cliente.getNumero(), cliente.getNombre(), Float.parseFloat(this.jtfSaldo.getText())));
    this.limpiarCampos();
}//GEN-LAST:event_jbGuardarCambiosActionPerformed

    private void jbBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbBuscarActionPerformed
       if(this.validarCampos()==true)
       {
           cliente=this.parent.buscarCliente(new Cliente(Long.parseLong(this.jtfNumero.getText()),"", 0));
           if (cliente!=null)
           {
               this.jtfSaldo.setEnabled(true);
               this.jbGuardarCambios.setEnabled(true);
               this.jtfSaldo.setText(String.valueOf(cliente.getSaldo()));
           }
           else
           {
               JOptionPane.showMessageDialog(null,"No existe el cliente", "Error", JOptionPane.ERROR_MESSAGE);
           }
         }      
         else
         {
             JOptionPane.showMessageDialog(null,"Lo ingresado no es numerico", "Error", JOptionPane.ERROR_MESSAGE);
         }
    }//GEN-LAST:event_jbBuscarActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        this.limpiarCampos();
    }//GEN-LAST:event_formWindowClosed

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JButton jbBuscar;
    private javax.swing.JButton jbGuardarCambios;
    private javax.swing.JTextField jtfNumero;
    private javax.swing.JTextField jtfSaldo;
    // End of variables declaration//GEN-END:variables

    public void limpiarCampos()
    {
        this.jtfNumero.setText("");
        this.jtfSaldo.setText("");
        this.jtfSaldo.setEnabled(false);
        this.jbGuardarCambios.setEnabled(false);
    }
    
    public boolean validarCampos()
    {
        if(this.isNumero()==false)
        {
            return false;
        }
        return true;
    }
    
    public boolean isNumero()
    {
        char c=' ';
        if(jtfNumero.getText().isEmpty()==false)
        {
            for(int i=0;i<jtfNumero.getText().length();i++)
            {
                c=jtfNumero.getText().charAt(i);
                if(Character.isDigit(c)==false)
                {
                    return false;
                }
            }
        }
        return true;
    }

}
